package com.baosight.sczx.zm.cp.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cp.ProducePlan;
import com.baosight.sczx.common.zm.cInterface.cp.ProfessionalPlan;
import com.baosight.sczx.common.zm.cInterface.cs.Engineering;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cp.domain.ZMCPProducePlanJJ;

/**
 * 掘进掘进计划明细表
 * @author wealupa
 * By.严洪浩
 */
public class ServiceZMCP13 extends ServiceEPBase {

	/**
	 * 初始化进入
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 查询
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		
		String fdProfessionalplanId = inInfo.getString("fdProfessionalplanId");
		String fdProduceplanId = inInfo.getString("fdProduceplanId");
		
		if(fdProfessionalplanId != null) {
			StaticTools.setQueryParameters(inInfo, "fdProfessionalplanId", inInfo.getString("fdProfessionalplanId"), true);//添加查询条件
		}

		if(fdProduceplanId != null) {
			StaticTools.setQueryParameters(inInfo, "fdProduceplanId", inInfo.getString("fdProduceplanId"), true);//添加查询条件
		}
		
		if(fdProfessionalplanId == null && fdProduceplanId == null) {
			return inInfo;
		}

		EiInfo outInfo = super.query(inInfo, "ZMCPProducePlanJJ.query", new ZMCPProducePlanJJ());		

		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", true, UserSession.getInSessionProperty("miningCode").toString()));	
		outInfo.addBlock(Engineering.getEngineeringForJJ("cmes.cp.engineering", "1", "", "1", true, UserSession.getInSessionProperty("miningCode").toString()));
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.mineRock", true));//煤岩类别
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.supportingType", true));//支护方式
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.roadwayUsing", true));//巷道用途
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.qualityGrade", true));//质量等级
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.grubProcess", true));//掘进方式
		outInfo.addBlock(StaticBlocks.GetEngineeringClassJJ(true)); // 工程类别
		outInfo.addBlock(StaticBlocks.getYesOrNo());//是否综掘巷道
		outInfo.addBlock(StaticBlocks.getZjType());//装渣方式 
		outInfo.addBlock(StaticBlocks.getPlanMonth("cmes.cp.planMonth", true));
		outInfo.addBlock(StaticBlocks.getPlanStatus("cmes.cp.planStatus", true));
		
		return outInfo;
	}
	
	/**
	 * 新增掘进计划明细
	 * 主表不等于草拟状态，不能新增
	 */
	@Override	
	public EiInfo insert(EiInfo inInfo) {
		//校验是否重复
	
		EiInfo out = new EiInfo(); 
		String fdPlanYear = inInfo.getString("fdPlanYear");
		String fdPlanMonth = inInfo.getString("fdPlanMonth");
		String fdProfessionalplanId = inInfo.getString("fdProfessionalplanId");
		String fdProduceplanId = inInfo.getString("fdProduceplanId");
		
		if(fdProfessionalplanId == null && fdProduceplanId == null)
			return inInfo;
		
		boolean checkProfessionalPlan = ProfessionalPlan.validate(fdProfessionalplanId);
		if(!checkProfessionalPlan) {
			out = this.query(inInfo);
			out.setMsg("当月的采煤专业计划已经上报或作废，不能新增！");
			return out;
		}
		
		boolean checkProducePlan = ProducePlan.validate(fdProduceplanId);
		if(!checkProducePlan) {
			out = this.query(inInfo);
			out.setMsg("当月的生产计划已经发布，不能新增！");
			return out;
		}
		
		ZMCPProducePlanJJ _dom = new ZMCPProducePlanJJ();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		boolean needUpdate = false;
		
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdProfessionalplanId", fdProfessionalplanId);
			params.put("fdPlanYear", fdPlanYear);
			params.put("fdPlanMonth", fdPlanMonth);
			params.put("fdProjectId", _dom.getFdProjectId());
			params.put("fdDepartmentId", _dom.getFdDepartmentId());
			boolean validate = this.validate(inInfo, "ZMCPProducePlanJJ.validate", params);
			if(validate) 
			{
				return inInfo;
			} 
			else{
				_dom.setFdDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdDepartmentId()));		//施工单位名称
				_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());				//创建部门ID
				_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
				_dom.setFdCreatorId(UserSession.getUserId().toString());
				_dom.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());
				if(fdProfessionalplanId != null) {
					_dom.setFdProfessionalplanId(fdProfessionalplanId);
				}
				else if(fdProduceplanId != null) {
					_dom.setFdProduceplanId(fdProduceplanId);
				}
				_dom.setFdPlanYear(inInfo.getString("fdPlanYear"));
				_dom.setFdPlanMonth(inInfo.getString("fdPlanMonth"));
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CP_PRODUCEPLAN_JJ"));
				
				dao.insert("ZMCPProducePlanJJ.insert", _dom);
				
				needUpdate = true;
		    }
		}
		
		if (needUpdate) {
			//只需更新专业计划主表的掘进相关数据
			//无需更新生产计划主表的数据
			//更新时先判断专业计划主表的状态
			
			if(fdProfessionalplanId != null) {
				ProfessionalPlan.updateByJJ(fdProfessionalplanId);
			}
			else if (fdProduceplanId != null){
				ProducePlan.updateByJJ(fdProduceplanId, null);
			}
		}

		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}
	
	/**
	 * 修改掘进计划明细
	 * 主表不等于草拟状态，不能修改
	 */
	@Override	
	public EiInfo update(EiInfo inInfo) {
		//校验是否重复
	
		EiInfo out = new EiInfo(); 
		String fdProfessionalplanId = inInfo.getString("fdProfessionalplanId");
		String fdProduceplanId = inInfo.getString("fdProduceplanId");
		
		if(fdProfessionalplanId == null && fdProduceplanId == null)
			return inInfo;
		
		boolean checkProfessionalPlan = ProfessionalPlan.validate(fdProfessionalplanId);
		if(!checkProfessionalPlan) {
			out = this.query(inInfo);
			out.setMsg("当月的采煤专业计划已经上报或作废，不能修改！");
			return out;
		}
		
		boolean checkProducePlan = ProducePlan.validate(fdProduceplanId);
		if(!checkProducePlan) {
			out = this.query(inInfo);
			out.setMsg("当月的生产计划已经发布，不能修改！");
			return out;
		}
		
		ZMCPProducePlanJJ _dom = new ZMCPProducePlanJJ();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		boolean needUpdate = false;
		
		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			params.put("fdProfessionalplanId", _dom.getFdProfessionalplanId());
			params.put("fdPlanYear", _dom.getFdPlanYear());
			params.put("fdPlanMonth", _dom.getFdPlanMonth());
			params.put("fdProjectId", _dom.getFdProjectId());
			params.put("fdDepartmentId", _dom.getFdDepartmentId());
			boolean validate = this.validate(inInfo, "ZMCPProducePlanJJ.validate", params);
			if(validate) 
			{
				return inInfo;
			} 
			else{
				_dom.setFdCreateDepartmentId(UserSession.getInSessionProperty("orgCode").toString());
				_dom.setFdCreateDepartment(UserSession.getInSessionProperty("orgName").toString());
				_dom.setFdCreatorId(UserSession.getUserId().toString());
				_dom.setFdCreator(UserSession.getInSessionProperty("UserCname").toString());				
				dao.update("ZMCPProducePlanJJ.update", _dom);
				
				needUpdate = true;
		    }
		}
		
		if (needUpdate) {
			//只需更新专业计划主表的掘进相关数据
			//无需更新生产计划主表的数据
			//更新时先判断专业计划主表的状态
			if(fdProfessionalplanId != null) {
				ProfessionalPlan.updateByJJ(fdProfessionalplanId);
			}
			else if (fdProduceplanId != null){
				ProducePlan.updateByJJ(fdProduceplanId, null);
			}
		}

		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;  
	}
	
	/**
	 * 删除掘进计划明细
	 * 主表不等于草拟状态，不能修改
	 */
	@Override	
	public EiInfo delete(EiInfo inInfo) {

		EiInfo _temp = new EiInfo();
		
		String fdProfessionalplanId = inInfo.getString("fdProfessionalplanId");
		String fdProduceplanId = inInfo.getString("fdProduceplanId");
		
		if(fdProfessionalplanId == null && fdProduceplanId == null)
			return inInfo;
		
		boolean checkProfessionalPlan = ProfessionalPlan.validate(fdProfessionalplanId);
		if(!checkProfessionalPlan) {
			_temp = this.query(inInfo);
			_temp.setMsg("当月的采煤专业计划已经上报或作废，不能删除！");
			return _temp;
		}
		
		boolean checkProducePlan = ProducePlan.validate(fdProduceplanId);
		if(!checkProducePlan) {
			_temp = this.query(inInfo);
			_temp.setMsg("当月的生产计划已经发布，不能删除！");
			return _temp;
		}

		_temp = super.delete(inInfo, "ZMCPProducePlanJJ.delete");
		
		//只需更新专业计划主表的掘进相关数据
		//无需更新生产计划主表的数据
		//更新时先判断专业计划主表的状态
		if(fdProfessionalplanId != null) {
			ProfessionalPlan.updateByJJ(fdProfessionalplanId);
		}
		else if (fdProduceplanId != null){
			ProducePlan.updateByJJ(fdProduceplanId, null);
		}
		
		EiInfo outInfo = this.query(inInfo);
        outInfo.setMsg(_temp.getMsg());
        outInfo.setDetailMsg(_temp.getDetailMsg());
		outInfo.setStatus(1);

		return outInfo;  
	}
	
	/**
	 * 验证每月只能一条记录
	 * @param inInfo
	 * @param _dom
	 * @param sqlName
	 * @param params
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	private boolean validate(EiInfo inInfo, String sqlName, Map<String, String> params) {
		if(params == null) {
			params = new HashMap<String, String>();
		}
		List result = dao.query(sqlName, params);
		if(result != null && result.size() > 0) {
			inInfo.setStatus(-1);
			String msg = "掘进计划每月同一施工部门针对同一工程仅可新增一份！";
			inInfo.setMsg(msg);
			return true;
		}
		return false;
	}
}
